#include "rmsqlmanager.h"
#include <QDebug>


RMSqlManager* RMSqlManager::gSQLInstance=NULL;
RMSqlManager::RMSqlManager(QObject *parent) : QObject(parent)
{
    gSQLInstance=this;
}

/*
    初始化数据库
*/
void RMSqlManager::initDB()
{
    //if (QSqlDatabase::contains("qt_sql_default_connection"))
    {
    //    database = QSqlDatabase::database("qt_sql_default_connection");
    }
    //else
    {
        database = QSqlDatabase::addDatabase("QSQLITE");
        database.setDatabaseName("RMDataBase.db");
        database.setUserName("ZhangYaoFa");
        database.setPassword("111111");
        qDebug() << "init db RMDataBase";
    }
}

bool RMSqlManager::openDB()
{
    if (!database.open())
    {
        qDebug() << "Error: Failed to connect database." << database.lastError();
    }
    else
    {
        //init tables
    }

    return true;
}

bool RMSqlManager::closeDB()
{
    database.close();
    return true;
}

int RMSqlManager::initTables()
{
    //创建用户表格 手机号 昵称 密码 角色
    QSqlQuery sql_query;
    if(!sql_query.exec("create table rm_count(phonenum text primary key,nickename text,password text,role text)"))
    {
        qDebug() << "Error: Fail to create table."<< sql_query.lastError();
    }
    else
    {
        qDebug() << "Table created!";
    }

    sql_query.exec("INSERT INTO rm_count VALUES(\"15726621111\", \"SendZhang\",\"111111\",\"1\")");
    sql_query.exec("INSERT INTO rm_count VALUES(\"15726622222\", \"SendZhang\",\"111111\",\"1\")");
    sql_query.exec("INSERT INTO rm_count VALUES(\"15726623333\", \"SendZhang\",\"111111\",\"1\")");
    sql_query.exec("INSERT INTO rm_count VALUES(\"15726624444\", \"SendZhang\",\"111111\",\"1\")");
    sql_query.exec("INSERT INTO rm_count VALUES(\"15726625555\", \"SendZhang\",\"111111\",\"1\")");
    sql_query.exec("INSERT INTO rm_count VALUES(\"15726626666\", \"SendZhang\",\"111111\",\"1\")");
    sql_query.exec("INSERT INTO rm_count VALUES(\"15726627777\", \"SendZhang\",\"111111\",\"1\")");
    sql_query.exec("INSERT INTO rm_count VALUES(\"15726628888\", \"SendZhang\",\"111111\",\"1\")");
    sql_query.exec("INSERT INTO rm_count VALUES(\"15726629999\", \"SendZhang\",\"111111\",\"1\")");

    //创建会议表格 会议号 创建时间 结束时间 状态 创建人手机号
    if(!sql_query.exec("create table rm_meetingcount(meetingnum text primary key,starttime text,endtime text,status text,creator text)"))
    {
        qDebug() << "Error: Fail to create table."<< sql_query.lastError();
    }
    else
    {
        qDebug() << "Table created!";
    }

    return 0;
}

int RMSqlManager::checkUserCount(QString phone,QString pwd)
{
    QSqlQuery sql_query;
    if(!sql_query.exec(QString("Select * From rm_count Where phonenum=%1").arg(phone)))
    {
        qDebug() << "Error: Fail to Select table rm_count."<< sql_query.lastError();
        return -1;
    }
    else
    {
//        if(sql_query.size()<=0)
//        {
//            qDebug() << "sql_query.size():"<<sql_query.size();
//            return -2;//no user
//        }
        while (sql_query.next()) {
            QString _localpwd = sql_query.value("password").toString();
            qDebug() << "_localpwd:"<<_localpwd;
            if(_localpwd == pwd)
            {
                return 0;
            }
            else
            {
                return -3;
            }
        }
    }
    return -4;
}
